{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# torchvision.utils\n",
    "## torchvision.utils.make_grid\n",
    "torchvision.utils.make_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale_each=False)  \n",
    "猜测，用来做雪碧图的（sprite image）。  \n",
    "给定 4D mini-batch Tensor， 形状为$B \\times C \\times H \\times W$,或者一个a list of image，做成一个size为(B / nrow, nrow)的雪碧图。  \n",
    "参数：  \n",
    "- normalize=True ，会将图片的像素值归一化处理\n",
    "- range - 如果 range=(min, max)， min和max是数字，那么min，max用来规范化image\n",
    "- scale_each=True ，每个图片独立规范化，而不是根据所有图片的像素最大最小值来规范化\n",
    "\n",
    "## torchvision.utils.save_image\n",
    "torchvision.utils.save_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale_each=False)  \n",
    "将给定的Tensor保存成image文件。如果给定的是mini-batch tensor，那就用make-grid做成雪碧图，再保存。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n",
      "Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to .//train//data\\cifar-10-python.tar.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\MyPythonWork\\Pytorch-Camp\\venv\\lib\\site-packages\\torchvision\\transforms\\transforms.py:208: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.\n",
      "  warnings.warn(\"The use of the transforms.Scale transform is deprecated, \" +\n",
      "12.2%"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torchvision.datasets as dset\n",
    "import torchvision.transforms as transforms\n",
    "import matplotlib.pyplot as plt\n",
    "import torch.nn as nn\n",
    "import torchvision\n",
    "import numpy as np\n",
    "%pylab inline\n",
    "dataset = dset.CIFAR10(root='.//train//data', download=True, \n",
    "                           transform=transforms.Compose([\n",
    "                           transforms.Resize(200),\n",
    "                           transforms.ToTensor(),\n",
    "                           transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n",
    "                        \n",
    "                           ]))\n",
    "img = torchvision.utils.make_grid(dataset[1][0]).numpy()\n",
    "plt.imshow(np.transpose(img,(1,2,0)))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
